package com.toobigtofail.enitities
{
	import com.toobigtofail.utility.Util;
	
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.Graphic;
	import net.flashpunk.Mask;
	import net.flashpunk.graphics.Canvas;
	import net.flashpunk.graphics.Image;
	
	public class Background extends Entity
	{
		[Embed(source="/assets/gfx/backgrounds/background1.png")] private static const B1_IMG:Class;
		[Embed(source="/assets/gfx/backgrounds/background2.png")] private static const B2_IMG:Class;
		[Embed(source="/assets/gfx/backgrounds/background3.png")] private static const B3_IMG:Class;
		[Embed(source="/assets/gfx/backgrounds/cavern.png")] private static const CAVERN_IMG:Class;
		
		private var backgroundArray:Array = new Array(CAVERN_IMG);
		private var bCanvas:Canvas = new Canvas(800,600);
		
		private var _background1:Image;
		private var _background2:Image;
		private var _background3:Image;
		private static const SCROLL_RATE_FACTOR:Number = 10;
		private static const BACKGROUND_LAYER:Number = 21;
		
		private var _offSetY:Number = 0;
		private var _speed:Number = 1;
		
		public function Background(x:Number=0, y:Number=0)
		{
			layer = BACKGROUND_LAYER;
			_background1 = new Image(backgroundArray[Util.random(0,backgroundArray.length-1)] as Class);
			_background2 = new Image(backgroundArray[Util.random(0,backgroundArray.length-1)] as Class);
			_background3 = new Image(backgroundArray[Util.random(0,backgroundArray.length-1)] as Class);
			this.x = x;
			this.y = y;
			bCanvas.drawGraphic(0,_offSetY,_background1);
			graphic = bCanvas;
			
		}
		
		override public function update():void
		{
			_offSetY -= FP.elapsed * SCROLL_RATE_FACTOR * _speed;
			if(_offSetY < -_background1.height)
			{
				_offSetY = 0;
				_background1 = _background2;
				_background2 = _background3;
				_background3 = new Image(backgroundArray[Util.random(0,backgroundArray.length-1)] as Class);
			}
			bCanvas.drawGraphic(0,_offSetY,_background1);
			bCanvas.drawGraphic(0,_offSetY + _background1.height, _background2);
			bCanvas.drawGraphic(0,_offSetY + _background1.height + _background2.height, _background3);
			super.update();
		}
		
		public function set speed(s:Number):void
		{
			_speed = s;
			
		}
		public function get speed():Number { return _speed; }
	}
}